package com.youngking.guns.modular.weixin;

import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.youngking.guns.GunsApplication;
import com.youngking.guns.core.base.controller.BaseController;
import com.youngking.guns.modular.system.dao.WxuserinfoMapper;
import com.youngking.guns.modular.system.model.Wxuserinfo;

import com.youngking.guns.modular.wxuserinfo.controller.WxuserinfoController;
import com.youngking.guns.modular.wxuserinfo.service.IWxuserinfoService;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.weixin4j.Weixin;
import org.weixin4j.WeixinException;
import org.weixin4j.model.message.Articles;
import org.weixin4j.model.message.OutputMessage;
import org.weixin4j.model.message.event.*;
import org.weixin4j.model.message.output.TextOutputMessage;
import org.weixin4j.model.user.User;
import org.weixin4j.spi.IEventMessageHandler;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Component
@RequestMapping("/eventMessage")
public class CustomEventMessageHandler extends BaseController implements IEventMessageHandler  {
    /*@Autowired
    private IWxuserinfoService wxuserinfoService;
    @Autowired
    private WxuserinfoMapper wxuserinfoMapper;*/
    @Autowired
    private IWxuserinfoService  wxuserinfoService;


    private static ApplicationContext applicationContext;//启动类set入，调用下面set方法

    public static void setApplicationContext(ApplicationContext context) {
        applicationContext = context;
    }
    private OutputMessage allType(EventMessage msg) {
        TextOutputMessage out = new TextOutputMessage();
        out.setContent("你的消息已经收到！");
        return out;
    }

    @Override
    @RequestMapping("/subscribe")
    public OutputMessage subscribe(SubscribeEventMessage msg) {
        Weixin weixin=new Weixin();
        Wxuserinfo wxuserinfo = new Wxuserinfo();
        String openid = msg.getFromUserName();
        try {
            User user = weixin.user().info(openid);
            HashMap<String, Object> paramMap = new HashMap();
            paramMap.put("openid", user.getOpenid());
            paramMap.put("subscribe", Integer.parseInt(user.getSubscribe()));
            paramMap.put("nickname", user.getNickname());
            paramMap.put("sex", user.getSex());
            paramMap.put("language", user.getLanguage());
            paramMap.put("city", user.getCity());
            paramMap.put("province", user.getProvince());
            paramMap.put("country", user.getCountry());
            paramMap.put("headimgurl", user.getHeadimgurl());
            paramMap.put("unionid", user.getUnionid());
            paramMap.put("groupid", user.getGroupid());

            HttpUtil.post("http://www.ynyxgcxx.com/projectmanage/wxuserinfo/add", paramMap);

            //发送图文消息
            List<Articles> articlesList = new ArrayList<>();
            Articles articles=new Articles();
            articles.setTitle("公司简介");
            articles.setPicUrl("https://mmbiz.qpic.cn/mmbiz_jpg/LBwMia1zJk1gEGdCBdWibhFhibyHutVNsWONx9vhODK6zB0iceiaGb2DyscMWBzqwNyNF1L5MW7sUeCRrvCUvggy7Qw/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1");
            articles.setPicurl("https://mmbiz.qpic.cn/mmbiz_jpg/LBwMia1zJk1gEGdCBdWibhFhibyHutVNsWONx9vhODK6zB0iceiaGb2DyscMWBzqwNyNF1L5MW7sUeCRrvCUvggy7Qw/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1");
            articles.setUrl("https://mp.weixin.qq.com/s/HoN16YBIVlLZtBm-1-mRLg");
            articlesList.add(articles);
            weixin.message().customSendNews(openid,articlesList);

        } catch (WeixinException e) {
            e.printStackTrace();
        }
        TextOutputMessage out = new TextOutputMessage();
        out.setContent("");
        return out;
    }

    @Override
    public OutputMessage unSubscribe(UnSubscribeEventMessage msg) {
        String openid = msg.getFromUserName();
        HashMap<String, Object> map = new HashMap();
        map.put("openid",openid);
        map.put("subscribe",0);
        String listSize= HttpUtil.post("http://www.ynyxgcxx.com/projectmanage/wxuserinfo/updateSubscribe", map);
        TextOutputMessage out = new TextOutputMessage();
        out.setContent("");
        return out;
    }

    @Override
    public OutputMessage qrsceneSubscribe(QrsceneSubscribeEventMessage msg) {
        return null;
    }

    @Override
    public OutputMessage qrsceneScan(QrsceneScanEventMessage msg) {
        return null;
    }

    @Override
    public OutputMessage location(LocationEventMessage msg) {
        return null;
    }

    @Override
    public OutputMessage click(ClickEventMessage msg) {
        return null;
    }

    @Override
    public OutputMessage view(ViewEventMessage msg) {
        return null;
    }

    @Override
    public OutputMessage scanCodePush(ScanCodePushEventMessage msg) {
        return null;
    }

    @Override
    public OutputMessage scanCodeWaitMsg(ScanCodeWaitMsgEventMessage msg) {
        return null;
    }

    @Override
    public OutputMessage picSysPhoto(PicSysPhotoEventMessage msg) {
        return null;
    }

    @Override
    public OutputMessage picPhotoOrAlbum(PicPhotoOrAlbumEventMessage msg) {
        return null;
    }

    @Override
    public OutputMessage picWeixin(PicWeixinEventMessage msg) {
        return null;
    }

    @Override
    public OutputMessage locationSelect(LocationSelectEventMessage msg) {
        return null;
    }
}
